package com.mobilepay.moneypig.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.mobilepay.moneypig.config.Constant;
import com.mobilepay.moneypig.model.activity.mine.VipActivity;
import com.mobilepay.moneypig.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.WEIXIN_PAY);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {//成功
                ToastUtil.showShortToast(WXPayEntryActivity.this, "支付成功");
                Intent intent = new Intent();
                intent.setClass(WXPayEntryActivity.this, VipActivity.class);
//              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            } else {
                ToastUtil.showShortToast(WXPayEntryActivity.this, "支付失败");
                finish();
            }
        }
    }
}